package org.colcon.web.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;

import org.colcon.web.model.framework.IUser;

public class User extends Item implements IUser
{
	private static final long serialVersionUID = 1607528648316514525L;
	
	public static final String
	
		// arguments
		EMAIL = "email",
		PASSWORD = "password";

	private String email, password;
	
	/** full paths to titles */
	private Set<String> titles;
	
	public User()
	{
		this(new HashMap<String, String>());
	}
	
	public User(Map<String, String> args)
	{
		this(args.get(ID), args.get(EMAIL), args.get(PASSWORD));
	}
	
	public User(String id, String email, String password)
	{
		super(Model.getUserContainerPath(), id);
		
		this.email = email;
		this.password = password;
		
		titles = new HashSet<String>();
	}
	
	public String getEmail()
	{
		return email;
	}
	
	public String getText()
	{
		if(getTitles().length == 0)
			return "no titles";
		
		String text = Model.getTitleString(getTitles()[0]);
		
		for(int i = 1; i < getTitles().length; i++)
			text += ", " + Model.getTitleString(getTitles()[i]);
		
		return text;
	}
	
	public boolean authenticate(String password)
	{
		return this.password.equals(password);
	}
	
	public String[] getTitles()
	{
		return titles.toArray(new String[0]);
	}
	
	public boolean hasTitle(String id)
	{
		return titles.contains(id);
	}
	
	public boolean addTitle(String id)
	{
		return titles.add(id);
	}
	
	public boolean removeTitle(String id)
	{
		return titles.remove(id);
	}
}
